package com.cdf.iapp.fragment;

import java.util.ArrayList;
import java.util.List;

import com.cdf.iapp.MainActivity;
import com.cdf.iapp.R;
import com.cdf.iapp.adapter.ChatAdapter;
import com.cdf.iapp.bean.PersonChat;
import com.cdf.iapp.netty.Client;
import com.cdf.iapp.netty.ClientSingle;

import android.animation.Animator;
import android.app.Activity;
import android.app.Fragment;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainFrag4 extends Fragment {

	private ChatAdapter chatAdapter;
	private ListView lv_chat_dialog;
	private List<PersonChat> personChats = new ArrayList<PersonChat>();
	public void addPersonChat(PersonChat personChat) {
		this.personChats.add(personChat);
	}

	public Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			int what = msg.what;
			switch (what) {
			case 1:
				/**
				 * ListView条目控制在最后一行
				 */
				lv_chat_dialog.setSelection(personChats.size());
				break;

			default:
				break;
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.frag_four, null);

		/**
		 * 虚拟4条发送方的消息
		 */

		// if(personChats.size()==0){
		// PersonChat personChat = new PersonChat();
		// personChat.setMeSend(false);
		// personChat.setChatMessage("本机默认消息");
		// personChats.add(personChat);
		// }

		lv_chat_dialog = (ListView) v.findViewById(R.id.lv_chat_dialog);
		Button btn_chat_message_send = (Button) v
				.findViewById(R.id.btn_chat_message_send);
		final EditText et_chat_message = (EditText) v
				.findViewById(R.id.et_chat_message);
		/**
		 * setAdapter
		 */
		chatAdapter = new ChatAdapter(getActivity(), personChats);
		lv_chat_dialog.setAdapter(chatAdapter);
		/**
		 * 发送按钮的点击事件
		 */
		btn_chat_message_send.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (TextUtils.isEmpty(et_chat_message.getText().toString())) {
					Toast.makeText(getActivity(), "发送内容不能为空", 0).show();
					return;
				}
				String txtMsg = et_chat_message.getText().toString();
				PersonChat personChat = new PersonChat();
				// 代表自己发送
				personChat.setMeSend(true);
				// 得到发送内容
				personChat.setChatMessage(txtMsg);
				// 加入集合
				personChats.add(personChat);
				// 清空输入框
				et_chat_message.setText("");
				// 刷新ListView
				chatAdapter.notifyDataSetChanged();
				handler.sendEmptyMessage(1);
				Client client = ClientSingle.getClient();
				try {
					client.sendData(txtMsg);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		return v;
	}
	
	public void refush() {
		if(chatAdapter!= null){
			chatAdapter.notifyDataSetChanged();	
			handler.sendEmptyMessage(1);
		}
	    
	}

	

}
